import Vue from "vue";
import VueRouter, { RouteConfig } from "vue-router";
import Application from "@/views/Application.vue";

import { getAccessToken } from "@/common/request";

Vue.use(VueRouter);

const routes: Array<RouteConfig> = [
  {
    path: "/",
    name: "Application",
    redirect: "/start"
  },
  {
    path: "/start",
    name: "Start",
    component: () => import("@/views/Start.vue")
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/Login.vue")
  },
  {
    path: "*",
    name: "NotFound",
    redirect: "/start"
  }
];

export const menuListDta = routes[0].children;

const router = new VueRouter({
  mode: "history",
  base: "/",
  routes
});

//注册一个全局前置守卫,确保要调用 next 方法，否则钩子就不会被 resolved
router.beforeEach((to, from, next) => {
  // if (to.meta.title)
  //   document.title = `${to.meta.title}`;
  // //路由切换时，如果没有就添加，有就跳过执行，添加固定参数
  // console.log(to.path);
  // if (to.path != "/login" && !getAccessToken()) {
  //   // next({
  //   //   name: "Login",
  //   //   query: {
  //   //     forwardUrl: encodeURIComponent(to.path)
  //   //   }
  //   // });
  //   next();
  // } else {
  //   next();
  // }
  next();
});

export default router;
